package com.smartcruise.main.mainFragment.tools.globalSearch;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cunoraz.tagview.Tag;
import com.cunoraz.tagview.TagView;
import com.smartcruise.R;
import com.smartcruise.base.BaseFragment;
import com.smartcruise.task.taskSearch.SearchContract;
import com.smartcruise.util.StringUtils;
import com.smartcruise.util.customView.ClearableEditTextWithIcon;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by FengChaoQun
 * on 2018/4/9 0021
 * 全局搜索界面
 */

public class GlobalSearchFragment extends BaseFragment implements SearchContract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.taskSearchLay)
    LinearLayout taskSearchLay;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.tagView)
    TagView tagView;
    Unbinder unbinder;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.searchInput)
    ClearableEditTextWithIcon searchInput;

    private SearchContract.Presenter mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getRootView(R.layout.fragment_search);
        unbinder = ButterKnife.bind(this, rootView);
        setCloseActivity();
        init();
        return rootView;
    }

    private void init() {

        mPresenter = new GlobalSearchPresenterImp(this);
        mPresenter.start();

        tagView.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(Tag tag, int i) {
                showToast(tag.text);
            }
        });

        searchInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isNotEmpty(searchInput)) {
                    hideSearchResult();
                    tagView.setVisibility(View.VISIBLE);
                }
            }
        });

        searchInput.setHint(R.string.inputSearchKey);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.back)
    public void onBackClicked() {
        getActivity().finish();
    }

    @OnClick(R.id.search)
    public void onSearchClicked() {
        if (StringUtils.isNotEmpty(searchInput)) {
            mPresenter.search(searchInput.getText().toString());
        } else {
            showToast(getString(R.string.searchContentNotAllowEmpty));
        }
    }

    @Override
    public void showTag(List<Tag> tags) {
        hideTag();
        tagView.setVisibility(View.VISIBLE);
        tagView.removeAll();
        tagView.addTags(tags);
    }

    @Override
    public void hideTag() {
        tagView.setVisibility(View.GONE);
    }

    @Override
    public void showSearchResult(BaseAdapter baseAdapter) {
        hideTag();
        listView.setVisibility(View.VISIBLE);
        listView.setAdapter(baseAdapter);
        if (baseAdapter.isEmpty()) {
            showToast("没有搜索到相关内容");
        }
    }

    @Override
    public void hideSearchResult() {
        listView.setVisibility(View.GONE);
    }
}
